package com.caine.androidtask.ls34;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.caine.androidtask.R;

public class HandlerActivity extends Activity {

	boolean flag = true;
	MyThread mthread = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.caine_ls34);
		
		Button btn_start = (Button) findViewById(R.id.btn_start);
		Button btn_stop = (Button) findViewById(R.id.btn_stop);
		mthread = new MyThread();
		btn_start.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				new Thread(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						mhandler.sendEmptyMessage(0X01);
					}
				}).start();
			}
		});
		
		btn_stop.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				new Thread(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						mhandler.sendEmptyMessage(0X02);
					}
				}).start();
				
			}
		});
	}
	
	Handler handler = new Handler();
	
	Handler mhandler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			switch(msg.what){
			case 0X01:
				handler.post(mthread);
				break;
			case 0X02:
				handler.removeCallbacks(mthread);
				break;
			}
		}
		
	};
	
	//Thread mThread = new Thread(runnable);
	
	public  class MyThread implements Runnable{

		@Override
		public void run() {
			// TODO Auto-generated method stub
				try {
					Thread.sleep(5000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				Log.e("CaineZhu", "CaineZhu : screen off");
		}
		
	}
}